package weaver.interfaces.kq.allowance;

import com.engine.kq.biz.KQWorkTime;
import weaver.common.StringUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class KqAllowanceJob extends BaseCronJob {
    BaseBean basebean=new BaseBean();
    @Override
    public void execute() {
        Map<String,String> resources=new HashMap<>();
        RecordSet rs=new RecordSet();

        LocalDate now = LocalDate.now();
        LocalDate yesterday = now.minusDays(1);
        String test="select distinct bm from uf_overtime_dt1 where mainid=?";
        rs.executeQuery(test,"6");
        int counts=0;
        while (rs.next()) {
            counts = rs.getCounts();
        }
        System.out.println("counts = " + counts);
        String querySql="select id from hrmresource";
        rs.executeQuery(querySql);
        while (rs.next()){
            String id= Util.null2String(rs.getString("id"));
            if (StringUtil.isNotNull(id)){
                resources.put(id,yesterday+"");
            }
        }
        try {
            getDailyFlowAllowanceData(resources);
        } catch (ParseException e) {
            basebean.writeLog(e);
        }

    }

    /**
     * 统计每日津贴数据
     * @return
     */
    public void getDailyFlowAllowanceData(Map<String,String> resources) throws ParseException {
        Map<String,Object> datas = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
        final Date OUTTIME_FIRST = sdf2.parse("20:00:00");
        final Date OUTTIME_SECOND = sdf2.parse("23:30:00");
        final Date OUTTIME_THIRD = sdf2.parse("09:00:00");
        final Date OUTTIME_FOURTH = sdf2.parse("12:00:00");

        KQWorkTime kqWorkTime = new KQWorkTime();

        RecordSet rs = new RecordSet();
        try{
            for(String rid : resources.keySet()){
                String kqdate = Util.null2String(resources.get(rid));
                if (StringUtil.isNotNull(kqdate)){
                    String queryKqSql="select resourceid,kqdate,signindate+ ' ' + signintime AS signindatetime,signoutdate+ ' ' + signouttime AS signoutdatetime "+
                            " from kq_format_detail "+
                            " where resourceid = ? and kqdate= ?";
                    rs.executeQuery(queryKqSql,rid,"2023-04-06");
                    while (rs.next()){
                        String id= Util.null2String(rs.getString("resourceid"));
                        String belongdate= Util.null2String(rs.getString("kqdate"));
                        //restDay-休息日；workDay-工作日
                        String signindatetimeStr = Util.null2String(rs.getString("signindatetime"));
                        String signoutdatetimeStr = Util.null2String(rs.getString("signoutdatetime"));

                        if (!weaver.general.StringUtil.isEmpty(signindatetimeStr) && !weaver.general.StringUtil.isEmpty(signoutdatetimeStr)) {
                            Date signindatetime = sdf.parse(signindatetimeStr);
                            Date signoutdatetime = sdf.parse(signoutdatetimeStr);
                            String signday = signindatetimeStr.substring(0, 10);
                            String outtimeStr = signoutdatetimeStr.substring(11, signoutdatetimeStr.length());
                            String intimeStr = signindatetimeStr.substring(11, signindatetimeStr.length());
                            Date outtime = sdf2.parse(outtimeStr);
                            Date intime = sdf2.parse(intimeStr);
                            boolean isWorkDay = kqWorkTime.isWorkDay(id, signday);
                            if (signindatetime.compareTo(signoutdatetime) <= 0) {
                                if (isWorkDay) {
                                    if (outtime.compareTo(OUTTIME_FIRST) >= 0) {
                                        String workdayallowance1 = Util.null2String(datas.get(id + "|" + belongdate + "|" + "workdayallowance_1"));
                                        if (weaver.general.StringUtil.isEmpty(workdayallowance1)) {
                                            kqCount(id,belongdate,"workdayallowance_1");

                                        }
                                    }
                                    if (outtime.compareTo(OUTTIME_SECOND) >= 0) {
                                        String workdayallowance2 = Util.null2String(datas.get(id + "|" + belongdate + "|" + "workdayallowance_2"));
                                        if (weaver.general.StringUtil.isEmpty(workdayallowance2)) {
                                            kqCount(id,belongdate,"workdayallowance_2");

                                        }
                                    }
                                } else {
                                    if (intime.compareTo(OUTTIME_THIRD) <= 0 && outtime.compareTo(OUTTIME_FOURTH) >= 0) {
                                        String restdayallowance1 = Util.null2String(datas.get(id + "|" + belongdate + "|" + "restdayallowance_1"));
                                        if (weaver.general.StringUtil.isEmpty(restdayallowance1)) {
                                            kqCount(id,belongdate,"restdayallowance_1");
                                        }
                                    }
                                    if (outtime.compareTo(OUTTIME_SECOND) >= 0) {
                                        String restdayallowance2 = Util.null2String(datas.get(id + "|" + belongdate + "|" + "restdayallowance_2"));
                                        if (weaver.general.StringUtil.isEmpty(restdayallowance2)) {
                                            kqCount(id,belongdate,"restdayallowance_2");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

        }catch (Exception e){
            basebean.writeLog(e);
        }

    }

    public void kqCount(String id, String kqdate,String allowanceType){
        RecordSet rs=new RecordSet();
        String updateSql="UPDATE kq_format_total set "+allowanceType+" = ? where resourceid = ? and kqdate = ?";
        rs.executeUpdate(updateSql,"1",id,kqdate);
    }

}
